import json

from apps.settingApi.api.timerTask.apiList.logsAdd import add
from apps.settingApi.api.timerTask.apiList.u9capiUtils import u9cApiList
from apps.settingApi.api.timerTask.apiList.wmsapiUtils import wmsApiList
from apps.settingApi.switchCase.casePrimaryKey import caseGetPrimaryKeyData
from comm.dbConnect import get_yun_session, dbListJon, dbJonOne, dbEdit
from comm.openDbConfig import openDbBase


# 基础资料
def u9capiBasic(db, taskObj):
    print('u9capi共用api任务;任务名称', taskObj['name'])
    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(None, db_config['u9capi'])

    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    # print(db_pull_config)
    # print(db_pull_session)
    # sql = "select top 5 prd_no,name,spc from prdt"
    # data,err = dbListJon(sql=sql,engineName=None,parm=None,db_session=db_pull_session)
    # print(data)
    # print(err)
    # for itm in taskObj['taskList']:
    #     if itm['type']=='推送':
    #         db_session = db
    #     else:
    #         db_session = db_pull_session
    #     mf_data,err = dbListJon(sql=itm["mf"], engineName=None, parm=None, db_session=db_session)
    #     if err!=None:
    #         logData['status'] = False
    #         logData['type'] = itm['type']
    #         logData['rem'] = err
    #         add(db, logData)
    #         continue

    # logData = {}
    # logData['status'] = True
    # logData['name'] = taskObj["name"]
    # logData['order'] = 'prd'+ caseGetPrimaryKeyData()
    # data = {
    #     "prd_no":logData['order'],
    #     "prd_name":"*****" + caseGetPrimaryKeyData()
    # }
    # logData['data'] = json.dumps(data,ensure_ascii=False)
    # add(db,logData)
    return

# 货品基础资料
def u9capiPrdt(db, taskObj):
    print('u9capi货品基础推送资料')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询需要推送的数据
    sql_select = """
select top 50 isnull(e2.code,'') as category_code, cast(isnull(a.Weight,0) as float) as weight,d.code as warehouse_code, a.InventorySecondUOM,
a.PurchaseUOM,a.id,a.code,a.org,b.name as unit_name,
case when a.itemformattribute=10 then 2 when a.itemformattribute=9 then 5 else 3 end as knd,
a.specs as spc,a.name as name,case when isnull(c.LotParam,'')='' then 'F' else 'T' end as batch_control,
a.SPECS as specifications from cbo_itemmaster  a WITH(NOLOCK)
left join base_uom_trl b WITH(NOLOCK) on a.manufactureuom=b.id and b.sysmlflag='zh-cn'
left join CBO_InventoryInfo c WITH(NOLOCK) on c.ItemMaster=a.id
left join CBO_Wh d WITH(NOLOCK) on c.Warehouse=d.id
left join CBO_ItemCategory e1 on e1.item=a.id and e1.CategoryKind='0'
left join cbo_category e2 on e2.ID=e1.Category
where 
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')='' or 
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.ModifiedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')) or
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.CreatedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')))
and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select prd_no from prdt where prd_no=:code"
    # # 更新推送时间
    sql_u = "update cbo_itemmaster set descflexfield_pubdescseg50=isnull(convert(varchar(19), GETDATE(), 120),'') where code=:code"

    # 1、查询需要推送的数据
    data, err = dbListJon(sql=sql_select, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询推送数据列表异常' + sql_select + err
        add(db, logData)
        return
    for itm in data:
        oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询货品' + sql_z + itm["code"] + '异常;' + err
            add(db, logData)
            continue
        addData = {}
        addData["owner_code"] = "OWNER"
        addData["item_properties"] = "001"
        addData["category_code"] = "0001"
        addData["unit_name"] = itm["unit_name"]
        addData["item_code"] = itm["code"]
        addData["item_name"] = itm["name"]
        addData["batch_control"] = itm["batch_control"]
        if itm["category_code"] !="":
            addData["category_code"] = itm["category_code"]
        addData["warehouse_code"] = itm["warehouse_code"]
        addData["specifications"] = itm["specifications"]
        addData["weight"] = itm["weight"]
        addData["kinds"] = itm["knd"]
        addData['extend_props'] = {
            "U9CID": itm["id"]
        }

        # 更新推送标识
        _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '更新料品' + itm["code"] + '推送标识异常;' + err
            add(db, logData)
            continue

        logData['order'] = itm["code"]

        logData['data'] = json.dumps(addData, ensure_ascii=False)
        if oneData is None:
            # 不存存在需要新增
            print('不存在')
            logData['rem'] = '新增'
            _, err = wmsApiList.addPrdt(addData)
            if err != None:
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        else:
            # 存在需要更新
            print('存在')
            logData['rem'] = '数据异动更新'
            _, err = wmsApiList.apiComm(addData, 'SYS/UpdateProduct')
            if err != None:
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        # # 更新推送标识
        # u_u9c = [
        #     {
        #         "Org": {
        #             "ID": org,
        #         },
        #         "Code": itm["code"],
        #         "Attributes": [
        #             {
        #                 "AttributeName": "DescFlexField.PubDescSeg50",
        #                 "AttributeValue": nowData(),
        #             }
        #         ],
        #     }
        # ]
        # u9c_, err = u9cApiList.prdtUpdate(u_u9c)
        if err != None:
            logData['status'] = False
            logData['rem'] = '货品资料推送成功但更推送标识送失败' + itm["code"]+ err
            add(db, logData)
            continue
        logData['status'] = True
        logData['rem'] = ""
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(addData, ensure_ascii=False)
        add(db, logData)
    return


# 中类
def u9capiIdx(db, taskObj):
    print('u9capi中类资料')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询需要推送的数据
    sql_select = """
select top 50 a.org,a.id,a.code,a.descflexfield_pubdescseg50,b.name,'00000000' as parentid from cbo_category a WITH(NOLOCK)
left join cbo_category_trl b WITH(NOLOCK) on a.id=b.id and b.sysmlflag='zh-cn'
where
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')='' or 
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.ModifiedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')) or
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.CreatedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),''))) and a.org='{0}'""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select idx_no from indx where idx_no=:code"
    # 更新推送时间
    sql_u = "update cbo_category set descflexfield_pubdescseg50=isnull(convert(varchar(19), GETDATE(), 120),'') where code=:code"

    # 1、查询需要推送的数据
    data, err = dbListJon(sql=sql_select, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询推送数据列表异常' + sql_select + err
        add(db, logData)
        return
    for itm in data:
        oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询中类' + sql_z + itm["code"] + '异常;' + err
            add(db, logData)
            continue
        # 更新推送标识
        _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '更新中类' + itm["code"] + '推送标识异常;' + err
            add(db, logData)
            continue
        addData = {}
        addData["owner_code"] = "OWNER"
        addData["category_code"] = itm["code"]
        addData["category_name"] = itm["name"]
        addData["category_upper"] = itm["parentid"]
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(addData, ensure_ascii=False)
        if oneData is None:
            # 不存存在需要新增
            print('不存在')
            logData['rem'] = '新增'
            _, err = wmsApiList.apiComm(addData, 'SYS/CreateProductCategory')
            if err != None:
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        else:
            # 存在需要更新
            print('存在')
            logData['rem'] = '数据异动更新'
            _, err = wmsApiList.apiComm(addData, 'SYS/UpdateProductCategory')
            if err != None:
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        logData['status'] = True
        # 推送成功回写记录
        logData['order'] = itm["code"]
        itmData = {
            "code": itm["code"],
            "name": itm["name"]
        }
        logData['rem'] = ""
        logData['status'] = True
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(itmData, ensure_ascii=False)
        add(db, logData)
    return


def u9capiDept(db, taskObj):
    print('u9capi部门资料')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询需要推送的数据
    sql_select = """
select top 50 a.id,a.org, case when isnull(a1.code,'')='' then '00000000' else a1.code end as ParentID,
a.code,case when isnull(b.name,'')='' then a.code else b.name end as name from CBO_Department a WITH(NOLOCK)
left join CBO_Department as a1 WITH(NOLOCK) on a.ParentNode=a1.id
left join CBO_Department_trl b WITH(NOLOCK) on a.id=b.id
where (isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')='' or 
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.ModifiedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')) or
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.CreatedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),''))) and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select dep from dept where dep=:code"
    # 更新推送时间
    sql_u = "update CBO_Department set descflexfield_pubdescseg50=isnull(convert(varchar(19), GETDATE(), 120),'') where id=:id"

    # 1、查询需要推送的数据
    data, err = dbListJon(sql=sql_select, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询推送数据列表异常' + sql_select + err
        add(db, logData)
        return
    for itm in data:
        oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询部门' + sql_z + itm["code"] + '异常;' + err
            add(db, logData)
            continue
        # 更新推送标识
        _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '更新部门' + itm["code"] + '推送标识异常;' + err
            add(db, logData)
            continue
        # descflexfield_pubdescseg50 = nowData()
        addData = {}
        # addData["owner_code"] = "OWNER"
        addData["department"] = itm["code"]
        addData["department_name"] = itm["name"]
        addData["department_upper"] = itm["parentid"]
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(addData, ensure_ascii=False)
        if oneData is None:
            # 不存存在需要新增
            print('不存在')
            logData['rem'] = '新增'
            _, err = wmsApiList.apiComm(addData, 'SYS/CreateDepartment')
            if err != None:
                print(err)
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        else:
            # 存在需要更新
            print('存在')
            logData['rem'] = '数据异动更新'
            _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
            if err != None:
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        # 推送成功回写记录
        logData['rem'] = ""
        logData['status'] = True
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(addData, ensure_ascii=False)
        add(db, logData)
    return

# 库位推送
def u9capiWh(db, taskObj):
    print('u9capi库位资料')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询需要推送的数据
    sql_select = """
select top 50 a.id,a.org,a.code,b.name, '00000000' as warehouse_upper
from CBO_Wh a WITH(NOLOCK)
left join CBO_Wh_Trl b WITH(NOLOCK) on a.id=b.id and b.SysMLFlag='zh-CN'
where
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')='' or 
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.ModifiedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')) or
(isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')!='' and isnull(convert(varchar(19), a.CreatedOn, 120),'')>=isnull(convert(varchar(19), a.descflexfield_pubdescseg50, 120),'')))
and  a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select wh from my_wh where wh=:code"
    # 更新推送时间
    sql_u = "update CBO_Wh set descflexfield_pubdescseg50=isnull(convert(varchar(19), GETDATE(), 120),'') where id=:id"
    # sql_wmsid = "update my_wh_z set u9cid=:id where wh=:code"
    sql_wmsid = """
IF EXISTS (SELECT wh FROM my_wh_z WHERE wh = :code)
BEGIN
    UPDATE my_wh_z
    SET 
        u9cid = :id
    WHERE wh = :code;
END
ELSE
BEGIN
    INSERT INTO my_wh_z (wh, u9cid)
    VALUES (:code, :id);
END
    
    """

    # 1、查询需要推送的数据
    data, err = dbListJon(sql=sql_select, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询推送数据列表异常' + sql_select + err
        add(db, logData)
        return
    for itm in data:
        oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询库位' + sql_z + itm["code"] + '异常;' + err
            add(db, logData)
            continue
        # 更新推送标识
        _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '更新库位' + itm["code"] + '推送标识异常;' + err
            add(db, logData)
            continue
        # descflexfield_pubdescseg50 = nowData()
        addData = {}
        # addData["owner_code"] = "OWNER"
        addData["warehouse_code"] = itm["code"]
        addData["warehouse_name"] = itm["name"]
        addData["warehouse_upper"] = itm["warehouse_upper"]
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(addData, ensure_ascii=False)
        if oneData is None:
            # 不存存在需要新增
            print('不存在')
            logData['rem'] = '新增'
            _, err = wmsApiList.apiComm(addData, 'SYS/CreateWarehouse')
            if err != None:
                print(err)
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
            _, err = dbEdit(sql=sql_wmsid, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新库位' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
        else:
            # 存在需要更新
            print('存在')
            logData['status'] = False
            logData['rem'] = '数据异动更新'
            _, err = dbEdit(sql=sql_wmsid, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新库位' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            _, err = wmsApiList.apiComm(addData, 'SYS/UpdateWarehouse')
            if err != None:
                logData['status'] = False
                logData['rem'] = err
                add(db, logData)
                continue
        logData['rem'] = ""
        logData['status'] = True
        logData['order'] = itm["code"]
        logData['data'] = json.dumps(addData, ensure_ascii=False)
        add(db, logData)
    return


# 采购入库   -----------------------此流程暂停使用
def u9capiPoTi(db, taskObj):
    print('u9capi采购入库送检-入库-标准收货')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code from pm_receivement a WITH(NOLOCK)
left join pm_rcvline b WITH(NOLOCK) on a.id= b.receivement
where isnull(a.descflexfield_pubdescseg50,'')!='T'
and b.SplitFlag IN (2,0) and isnull(b.id,'')!='' and a.Status in('2','3')  and a.RtnType=0
and a.org='{0}'
group by a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep, 'TI' as request_id, a.id ,'OWNER' as owner_code,'01' as order_type,a.id as oth_task_id,'u9c' as ref_id, CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), b.ArrivedTime, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,case when isnull(e.code,'')='' then '00000000' else e.code end as department,
f.code as supplier_code,f.shortname as supplier_name,'{1}' as operator_code, '' as source_bilno from pm_receivement a WITH(NOLOCK)
left join pm_rcvline b WITH(NOLOCK) on a.id= b.receivement
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.rcvdept=e.id
left join cbo_supplier f WITH(NOLOCK) on  a.supplier_supplier=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T' and b.SplitFlag IN (2,0) and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code,
f.code,f.shortname,CONVERT(VARCHAR(100), b.ArrivedTime, 23)
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select b.InvLotCode as batch_code,b.DocLineNo as itm,h1.id as source_bilitm,b.id as request_itm,h.docno as source_bilno,
h.id as u9sourceid,h.docno as u9sourceno, g.code as u9projectid,g1.name as u9projectnm,'PO' as source_bilid,'TI' as request_id,b.id,c.code as item_code,c.name as item_name,cast(b.EyeballingQtyTU as float) as qty,
a.docno as request_code,b.id as request_itm from pm_receivement a WITH(NOLOCK)
left join pm_rcvline b WITH(NOLOCK) on a.id= b.receivement
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.rcvdept=e.id
left join PM_PurchaseOrder h  WITH(NOLOCK) on h.id=b.SrcDoc_SrcDoc_EntityID
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
left join PM_POLine h1  WITH(NOLOCK) on b.SrcDoc_SrcDoc_EntityID=h1.PurchaseOrder and b.SrcDoc_SrcDocLine_EntityID = h1.id
where a.docno=:code and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and b.SplitFlag IN (2,0) and a.org='{0}'
""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update pm_receivement set descflexfield_pubdescseg50='T' where id=:id"

    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm={}, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询标准收货表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:

        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None, parm=itm_bin, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询标准收货表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询标准收货表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return

            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(code='pc', data = tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                continue
            add(db, logData)
    return


# 采购检验入库
def u9capiPoYsRk(db, taskObj):
    print('u9capi采购检验入库-检验-标准收货')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
#     sql_select_bin = """
# select  a.docno as code from pm_receivement a WITH(NOLOCK)
# left join pm_rcvline b WITH(NOLOCK) on a.id= b.receivement and isnull(b.Status,0) in (2,3) and isnull(b.EyeballingQtyTU,0) <= isnull(b.RcvQtyTU,0) + isnull(b.RtnFillQtyTU,0) - isnull(b.RtnDeductQtyTU,0)
# left join (select count(DISTINCT  RcvProcedure) as count_row,receivement from pm_rcvline where RcvProcedure!=2 group by receivement)c on a.id= c.receivement
# where isnull(a.descflexfield_pubdescseg50,'')!='T' and isnull(c.count_row,0)=0
# and b.SplitFlag IN (2,0) and b.RcvProcedure=2 and isnull(a.Status,0) in(0,3) and isnull(b.id,'')!=''
# and a.org='{0}'
# group by a.docno
#     """.format(org, userCode)
    sql_select_bin = """
select  a.docno as code from pm_receivement a WITH(NOLOCK)
left join (select receivement,sum(isnull(EyeballingQtyTU,0)) as qty1, sum(isnull(RcvQtyTU,0) + isnull(RtnFillQtyTU,0) - isnull(RtnDeductQtyTU,0)) as qty2 
from pm_rcvline WITH(NOLOCK) where  SplitFlag IN (2,0) and RcvProcedure=2 group by receivement) b  on a.id= b.receivement 
left join (select count(DISTINCT  RcvProcedure) as count_row,receivement from pm_rcvline where RcvProcedure!=2 group by receivement)c 
on a.id= c.receivement
where isnull(a.descflexfield_pubdescseg50,'')!='T' and isnull(c.count_row,0)=0 
 and isnull(a.Status,0) in(0,3) and isnull(b.receivement,'')!='' and b.qty1<=b.qty2
and a.org='{0}'
group by a.docno
        """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep, 'TI' as request_id, a.id ,'OWNER' as owner_code,'01' as order_type,a.id as oth_task_id,'u9c' as ref_id, CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), b.ArrivedTime, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,case when isnull(e.code,'')='' then '00000000' else e.code end as department,
f.code as supplier_code,f.shortname as supplier_name,'{1}' as operator_code, '' as source_bilno from pm_receivement a WITH(NOLOCK)
left join pm_rcvline b WITH(NOLOCK) on a.id= b.receivement and isnull(b.Status,0) in (2,3) and isnull(b.EyeballingQtyTU,0) <= isnull(b.RcvQtyTU,0) + isnull(b.RtnFillQtyTU,0) - isnull(b.RtnDeductQtyTU,0)
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.rcvdept=e.id
left join cbo_supplier f WITH(NOLOCK) on  a.supplier_supplier=f.id
where a.docno=:code and isnull(b.id,'')!='' and isnull(a.descflexfield_pubdescseg50,'')!='T' and b.SplitFlag IN (2,0) and b.RcvProcedure=2 and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code,
f.code,f.shortname,CONVERT(VARCHAR(100), b.ArrivedTime, 23)
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select b.DocLineNo as itm,b.DocLineNo as row_no,h1.id as source_bilitm,b.id as request_itm,h.docno as source_bilno,
h.id as u9sourceid,h.docno as u9sourceno, g.code as u9projectid,g1.name as u9projectnm,'PO' as source_bilid,'TI' as request_id,b.id,c.code as item_code,c.name as item_name,cast(b.EyeballingQtyTU as float) as qty,
a.docno as request_code,b.id as request_itm from pm_receivement a WITH(NOLOCK)
left join pm_rcvline b WITH(NOLOCK) on  a.id= b.receivement and isnull(b.Status,0) in (2,3) and isnull(b.EyeballingQtyTU,0) <= isnull(b.RcvQtyTU,0) + isnull(b.RtnFillQtyTU,0) - isnull(b.RtnDeductQtyTU,0)
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.rcvdept=e.id
left join PM_PurchaseOrder h  WITH(NOLOCK) on h.id=b.SrcDoc_SrcDoc_EntityID
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
left join PM_POLine h1  WITH(NOLOCK) on b.SrcDoc_SrcDoc_EntityID=h1.PurchaseOrder and b.SrcDoc_SrcDocLine_EntityID = h1.id
where a.docno=:code and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and isnull(b.id,'')!='' and b.SplitFlag IN (2,0) and b.RcvProcedure=2 and a.org='{0}'
""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update pm_receivement set descflexfield_pubdescseg50='T' where id=:id"

    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm={}, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询标准收货表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:

        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None, parm=itm_bin, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询标准收货表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询标准收货表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新部门' + itm["code"] + '推送日期异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return

            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(code='pc',data = tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
            # # 存在需要更新
            # print('存在')
            # logData['rem'] = '数据异动更新'
            # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
            # if err != None:
            #     logData['status'] = False
            #     logData['rem'] = err
            #     add(db, logData)
            #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 出货计划
def u9capiCk(db, taskObj):
    print('u9capi销售出货-出货计划')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select top 5  a.docno as code
from SM_ShipPlan a WITH(NOLOCK)
left join SM_ShipPlanLine b WITH(NOLOCK) on a.id= b.ShipPlan
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
where isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=3 and a.IsClosed=0
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select '' as dep,a.id,'CK' as request_id,a.id as oth_task_id,'OWNER' as owner_code,'01' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), b.RequireDate, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
'00000000'  as department,f.code as cus_no,f.shortname as cus_name,'{1}' as operator_code,
 b.SrcDoc as source_bilno from SM_ShipPlan a WITH(NOLOCK)
left join SM_ShipPlanLine b WITH(NOLOCK) on a.id= b.ShipPlan
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
--left join cbo_department e WITH(NOLOCK) on b.rcvdept=e.id
left join CBO_Customer f WITH(NOLOCK) on  b.OrderBy_Customer=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id,a.createdon,a.docno,d.code,f.shortname,f.code,b.SrcDoc,CONVERT(VARCHAR(100), b.RequireDate, 23)
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select b.DocLineNo as itm,'SO' as source_bilid,'CK' as request_id,b.id as u9sourceid,f.docno as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.PlanQtyTU as float) as qty,a.docno as request_code,
b.id as request_itm,f.docno as source_bilno from SM_ShipPlan a WITH(NOLOCK)
left join SM_ShipPlanLine b WITH(NOLOCK) on a.id= b.ShipPlan
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code and isnull(d.code,'')=:wh and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update SM_ShipPlan set descflexfield_pubdescseg50 = 'T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询出货计划表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询出货计划表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询出货计划表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(code='sa', data=tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 领料申请
def u9capiMlSq(db, taskObj):
    print('u9capi生产发料-领料申请')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from Issue_IssueApplyDoc a WITH(NOLOCK)
left join Issue_IssueApplyDocLine b WITH(NOLOCK) on a.id= b.IssueApplyDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo=c.id
where isnull(a.descflexfield_pubdescseg50,'')!='T' and a.ApplyType=0 and a.DocState=2
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,
'ML' as request_id,a.id,a.id as oth_task_id,
'OWNER' as owner_code,'05' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19),a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
case when isnull(e.code,'')='' then '00000000' else  e.code end as department,
'' as cus_no,'' as cus_name,'{1}' as operator_code,'' as source_bilno 
from Issue_IssueApplyDoc a WITH(NOLOCK)
left join Issue_IssueApplyDocLine b WITH(NOLOCK) on a.id= b.IssueApplyDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo=c.id
left join cbo_wh d WITH(NOLOCK) on b.SupplyWh=d.id
left join cbo_department e WITH(NOLOCK) on a.IssueDepartment=e.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T' and a.ApplyType=0
and a.org='{0}'
group by a.id,a.createdon,a.docno
,d.code,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select 'MO' as source_bilid,'ML' as request_id,f.id as u9sourceid,f.docno as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,f.id as source_bilitm,b.id,c.code as item_code,c.name as item_name,cast(b.IssuedApplyQtyRcvUOM as float) as qty,
a.docno as request_code,b.id as request_itm,f.docno as source_bilno from Issue_IssueApplyDoc a WITH(NOLOCK)
left join Issue_IssueApplyDocLine b WITH(NOLOCK) on a.id= b.IssueApplyDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo=c.id
left join cbo_wh d WITH(NOLOCK) on b.SupplyWh=d.id
left join cbo_department e WITH(NOLOCK) on a.IssueDepartment=e.id
left join MO_MO f WITH(NOLOCK) on  b.MO=f.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code and isnull(d.code,'')=:wh and isnull(e.code,'') =:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update Issue_IssueApplyDoc set descflexfield_pubdescseg50 = 'T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询领料申请表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询领料申请表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询领料申请表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm={"id":itm["id"]}, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 生产退料申请
# 退料申请
def u9capiMlSqTl(db, taskObj):
    print('u9capi生产发料-退料申请')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from Issue_IssueApplyDoc a WITH(NOLOCK)
left join Issue_IssueApplyDocLine b WITH(NOLOCK) on a.id= b.IssueApplyDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo=c.id
where isnull(a.descflexfield_pubdescseg50,'')!='T' and a.ApplyType=1 and a.DocState=2
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,
'ML' as request_id,a.id,a.id as oth_task_id,
'OWNER' as owner_code,'06' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19),a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
case when isnull(e.code,'')='' then '00000000' else  e.code end as department,CONVERT(VARCHAR(100), a.createdon, 23)  as expect_start_time,
'{1}' as operator_code,'' as source_bilno,'' as supplier_code,'' as supplier_name
from Issue_IssueApplyDoc a WITH(NOLOCK)
left join Issue_IssueApplyDocLine b WITH(NOLOCK) on a.id= b.IssueApplyDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo=c.id
left join cbo_wh d WITH(NOLOCK) on b.SupplyWh=d.id
left join cbo_department e WITH(NOLOCK) on a.IssueDepartment=e.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T' and a.ApplyType=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
,d.code,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select 'MO' as source_bilid,'M2' as request_id,f.id as u9sourceid,f.docno as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,f.id as source_bilitm,b.id,c.code as item_code,c.name as item_name,cast(b.IssuedApplyQtyRcvUOM as float) as qty,
a.docno as request_code,b.id as request_itm,f.docno as source_bilno from Issue_IssueApplyDoc a WITH(NOLOCK)
left join Issue_IssueApplyDocLine b WITH(NOLOCK) on a.id= b.IssueApplyDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo=c.id
left join cbo_wh d WITH(NOLOCK) on b.SupplyWh=d.id
left join cbo_department e WITH(NOLOCK) on a.IssueDepartment=e.id
left join MO_MO f WITH(NOLOCK) on  b.MO=f.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code and isnull(d.code,'')=:wh and isnull(e.code,'') =:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update Issue_IssueApplyDoc set descflexfield_pubdescseg50 = 'T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询退料申请表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询退料申请表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询退料申请表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm={"id":itm['id']}, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]
            print(addData)
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 进货回退申请
def u9capiPoTh(db, taskObj):
    print('u9capi退货申请')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from PM_Rtn a WITH(NOLOCK)
left join PM_RtnLine b WITH(NOLOCK) on a.id= b.Rtn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
where isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=2
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,
'PB' as request_id,a.id,a.id as oth_task_id,
'OWNER' as owner_code,'02' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19),a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
case when isnull(e.code,'')='' then '00000000' else  e.code end as department,
f.code as cus_no,f.shortname as cus_name,'{1}' as operator_code,'' as source_bilno 
from PM_Rtn a WITH(NOLOCK)
left join PM_RtnLine b WITH(NOLOCK) on a.id= b.Rtn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.WH=d.id
left join cbo_department e WITH(NOLOCK) on a.Dept=e.id
left join cbo_supplier f WITH(NOLOCK) on a.RtnToSupplier_Supplier=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T' 
and a.org='{0}'
group by a.id,a.createdon,a.docno
,d.code,e.code,f.code,f.shortname
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select 'SA' as source_bilid,'PB' as request_id,b.id as u9sourceid,f.docno as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,f.id as source_bilitm,b.id,
c.code as item_code,c.name as item_name,cast(b.RtnQtyPU as float) as qty,
a.docno as request_code,b.id as request_itm,f.docno as source_bilno from PM_Rtn a WITH(NOLOCK)
left join PM_RtnLine b WITH(NOLOCK) on a.id= b.Rtn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.WH=d.id
left join cbo_department e WITH(NOLOCK) on a.Dept=e.id
left join pm_receivement f WITH(NOLOCK) on  b.SrcDoc_SrcDoc_EntityID=f.id
left join PM_POLine f1 WITH(NOLOCK) on  b.SrcDoc_SrcDocLine_EntityID=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code and isnull(d.code,'')=:wh and isnull(e.code,'') =:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update PM_Rtn set descflexfield_pubdescseg50='T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询退货申请表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询退货申请表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询退货申请表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 销货退回
def u9capiSaTh(db, taskObj):
    print('u9capi退回处理')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code from SM_RMA a WITH(NOLOCK)
left join SM_RMALine b WITH(NOLOCK) on a.id= b.RMA
where isnull(a.descflexfield_pubdescseg50,'')!='T' 
and a.Status=3
and a.org='{0}'
group by a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep, 'SB' as request_id, a.id ,'OWNER' as owner_code,'03' as order_type,a.id as oth_task_id,'u9c' as ref_id, 
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,case when isnull(e.code,'')='' then '00000000' else e.code end as department,
f.code as supplier_code,f1.name as supplier_name,'{1}' as operator_code, '' as source_bilno from SM_RMA a WITH(NOLOCK)
left join SM_RMALine b WITH(NOLOCK) on a.id= b.RMA
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.Warehouse=d.id
left join cbo_department e WITH(NOLOCK) on a.SaleDept=e.id
left join CBO_Customer f WITH(NOLOCK) on  a.Customer_Customer=f.id
left join CBO_Customer_trl f1 WITH(NOLOCK) on  f.id=f1.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code,
f.code,f1.name
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select h1.id as source_bilitm,b.id as request_itm,h.docno as source_bilno,
h.id as u9sourceid,h.docno as u9sourceno, g.code as u9projectid,g1.name as u9projectnm,'SA' as source_bilid,'SB' as request_id,b.id,c.code as item_code,c.name as item_name,cast(b.ApplyQtyPU as float) as qty,
a.docno as request_code,b.id as request_itm from SM_RMA a WITH(NOLOCK)
left join SM_RMALine b WITH(NOLOCK) on a.id= b.RMA
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.Warehouse=d.id
left join cbo_department e WITH(NOLOCK) on a.SaleDept=e.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
left join SM_Ship h  WITH(NOLOCK) on h.docno=b.SrcDocNo
left join SM_ShipLine h1  WITH(NOLOCK) on b.SrcDocLineNo=h1.DocLineNo and h.id = h1.Ship
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update SM_RMA set descflexfield_pubdescseg50='T' where id=:id"

    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm={}, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询退回处理表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:

        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None, parm=itm_bin, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询退回处理表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询退回处理表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return

            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 废品处理---暂时无用
def u9capiFpCl(db, taskObj):
    print('u9capi废品出库-废品处理')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from InvDoc_WasterDisposeTrans a WITH(NOLOCK)
left join InvDoc_WasterDisposeTransL b WITH(NOLOCK) on a.id= b.WasterDisposeTrans
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
where isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,a.id,'XJ' as request_id,a.id as oth_task_id,'OWNER' as owner_code,'12' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
case when isnull(e.code,'')='' then '00000000' else e.code end  as department,'' as cus_no,'' as cus_name,'{1}' as operator_code,
 '' as source_bilno from InvDoc_WasterDisposeTrans a WITH(NOLOCK)
left join InvDoc_WasterDisposeTransL b WITH(NOLOCK) on a.id= b.WasterDisposeTrans
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  b.OrderBy_Customer=f.id
where isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id,a.createdon,a.docno,d.code,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select '' as source_bilid,'XJ' as request_id,'' as u9sourceid,'' as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,a.docno as request_code,
b.id as request_itm,'' as source_bilno from InvDoc_WasterDisposeTrans a WITH(NOLOCK)
left join InvDoc_WasterDisposeTransL b WITH(NOLOCK) on a.id= b.WasterDisposeTrans
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
--left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_WasterDisposeTrans set descflexfield_pubdescseg50='T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询废品处理表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询废品处理表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询废品处理表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新部门' + itm["code"] + '推送日期异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 杂发出库单
def u9capiZfCk(db, taskObj):
    print('u9capi杂发出库单')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from InvDoc_MiscShip a WITH(NOLOCK)
left join InvDoc_MiscShipL b WITH(NOLOCK) on a.id= b.MiscShip
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join InvDoc_MiscShipDocType d WITH(NOLOCK) on d.id=a.MiscShipDocType
where isnull(d.code,'')!='FPCL001' and isnull(a.descflexfield_pubdescseg50,'')!='T' and a.Status=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,a.id,'QCK' as request_id,a.id as oth_task_id,'OWNER' as owner_code,'16' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
case when isnull(e.code,'')='' then '00000000' else e.code end  as department,'' as cus_no,'' as cus_name,'{1}' as operator_code,
 '' as source_bilno from InvDoc_MiscShip a WITH(NOLOCK)
left join InvDoc_MiscShipL b WITH(NOLOCK) on a.id= b.MiscShip
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  b.OrderBy_Customer=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id,a.createdon,a.docno,d.code,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select b.LotInfo_LotCode as batch_code,'' as source_bilid,'QCK' as request_id,'' as u9sourceid,'' as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,a.docno as request_code,
b.id as request_itm,'' as source_bilno from InvDoc_MiscShip a WITH(NOLOCK)
left join InvDoc_MiscShipL b WITH(NOLOCK) on a.id= b.MiscShip
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
--left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_MiscShip set descflexfield_pubdescseg50='T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询杂发出库单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询杂发出库单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询杂发出库单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新部门' + itm["code"] + '推送日期异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 杂收入库单
def u9capiZsRk(db, taskObj):
    print('u9capi杂收单')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code from InvDoc_MiscRcvTrans a WITH(NOLOCK)
left join InvDoc_MiscRcvTransL b WITH(NOLOCK) on a.id= b.MiscRcvTrans
where isnull(a.descflexfield_pubdescseg50,'')!='T' 
and a.Status=1
and a.org='{0}'
group by a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep, 'QRK' as request_id, a.id ,'OWNER' as owner_code,'11' as order_type,a.id as oth_task_id,'u9c' as ref_id, 
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,case when isnull(e.code,'')='' then '00000000' else e.code end as department,
'' as supplier_code,'' as supplier_name,'{1}' as operator_code, '' as source_bilno from InvDoc_MiscRcvTrans a WITH(NOLOCK)
left join InvDoc_MiscRcvTransL b WITH(NOLOCK) on a.id= b.MiscRcvTrans
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  a.Customer_Customer=f.id
--left join CBO_Customer_trl f1 WITH(NOLOCK) on  f.id=f1.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select b.LotInfo_LotCode as batch_code,'' as source_bilitm,b.id as request_itm,''as source_bilno,
'' as u9sourceid,'' as u9sourceno, g.code as u9projectid,g1.name as u9projectnm,
'' as source_bilid,'QRK' as request_id,b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,
a.docno as request_code,b.id as request_itm from InvDoc_MiscRcvTrans a WITH(NOLOCK)
left join InvDoc_MiscRcvTransL b WITH(NOLOCK) on a.id= b.MiscRcvTrans
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
--left join PM_PurchaseOrder h  WITH(NOLOCK) on h.id=b.SrcDoc_EntityID
--left join PM_POLine h1  WITH(NOLOCK) on b.SrcDoc_EntityID=h1.PurchaseOrder and b.SrcDocLine_EntityID = h1.id
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_MiscRcvTrans set descflexfield_pubdescseg50='T' where id=:id"

    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm={}, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询杂收单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:

        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None, parm=itm_bin, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询杂收单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询杂收单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新部门' + itm["code"] + '推送日期异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return

            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 调拨出库----暂时无用了
def u9capiDbCk(db, taskObj):
    print('u9capi调拨出库单')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.Status,a.docno as code
from InvDoc_TransferOut a WITH(NOLOCK)
left join InvDoc_TransOutLine b WITH(NOLOCK) on a.id= b.TransferOut
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
where isnull(d.code,'')!='WWFL001' and isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select B.ID,isnull(e.code,'') as dep, 'DC' as request_id, a.id ,'OWNER' as owner_code,'17' as order_type,a.id as oth_task_id,'u9c' as ref_id, 
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh1,isnull(d1.code,'') as wh2,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code1,
case when isnull(d1.code,'')='' then '00000000' else d.code end as warehouse_code1,
case when isnull(e.code,'')='' then '00000000' else e.code end as department2,
'' as supplier_code,'' as supplier_name,'{1}' as operator_code, '' as source_bilno from InvDoc_TransferOut a WITH(NOLOCK)
left join InvDoc_TransOutLine b WITH(NOLOCK) on a.id= b.TransferOut
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.TransOutWh=d.id
left join InvDoc_TransOutSubLine dz WITH(NOLOCK) on b.id = dz.TransOutLine 
left join cbo_wh d1 WITH(NOLOCK) on dz.TransInWh=d1.id
left join cbo_department e WITH(NOLOCK) on b.SalesDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  a.Customer_Customer=f.id
--left join CBO_Customer_trl f1 WITH(NOLOCK) on  f.id=f1.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code,d1.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select '' as source_bilid,'DC' as request_id,'' as u9sourceid,'' as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,a.docno as request_code,
b.id as request_itm,'' as source_bilno from InvDoc_TransferOut a WITH(NOLOCK)
left join InvDoc_TransOutLine b WITH(NOLOCK) on a.id= b.TransferOut
left join cbo_itemmaster c WITH(NOLOCK) on b.iteminfo_itemid=c.id
left join cbo_wh d WITH(NOLOCK) on b.TransOutWh=d.id
left join InvDoc_TransOutSubLine dz WITH(NOLOCK) on b.id = dz.TransOutLine 
left join cbo_wh d1 WITH(NOLOCK) on dz.TransInWh=d1.id
left join cbo_department e WITH(NOLOCK) on b.SalesDept=e.id
--left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
--left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh1 and isnull(d1.code,'')=:wh2 and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update CBO_Department set descflexfield_pubdescseg50=convert(varchar(23), getdate(), 121) where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询调拨出库单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询调拨出库单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询调拨出库单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # # 更新推送标识
            # _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            # if err != None:
            #     logData['status'] = False
            #     logData['rem'] = '更新部门' + itm["code"] + '推送日期异常;' + err
            #     add(db, logData)
            #     continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 调拨入库
def u9capiDbRk(db, taskObj):
    print('u9capi调拨入库单*********************************************')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from InvDoc_TransferIn a WITH(NOLOCK)
left join InvDoc_TransInLine b WITH(NOLOCK) on a.id= b.TransferIn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join InvDoc_TransInDocType d WITH(NOLOCK) on d.id=a.TransInDocType
where isnull(d.code,'')!='WWFL001' and isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,a.id,'DR' as request_id,a.id as oth_task_id,'OWNER' as owner_code,'17' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh2,isnull(d1.code,'') as wh1,
CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code2,
case when isnull(d.code,'')='' then '00000000' else d1.code end as warehouse_code1,
case when isnull(e.code,'')='' then '00000000' else e.code end  as department,'' as cus_no,'' as cus_name,'{1}' as operator_code,
 '' as source_bilno from InvDoc_TransferIn a WITH(NOLOCK)
left join InvDoc_TransInLine b WITH(NOLOCK) on a.id= b.TransferIn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.TransInWh=d.id
left join InvDoc_TransInSubLine dz WITH(NOLOCK) on b.id = dz.TransInLine 
left join cbo_wh d1 WITH(NOLOCK) on dz.TransOutWh=d1.id
left join cbo_department e WITH(NOLOCK) on b.SalesDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  b.OrderBy_Customer=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id,a.createdon,a.docno,d.code,e.code,d1.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select '' as source_bilid,'DR' as request_id,'' as u9sourceid,'' as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,b.LotInfo_LotCode as batch_code,a.docno as request_code,
b.id as request_itm,'' as source_bilno from InvDoc_TransferIn a WITH(NOLOCK)
left join InvDoc_TransInLine b WITH(NOLOCK) on a.id= b.TransferIn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.TransInWh=d.id
left join InvDoc_TransInSubLine dz WITH(NOLOCK) on b.id = dz.TransInLine 
left join cbo_wh d1 WITH(NOLOCK) on dz.TransOutWh=d1.id
left join cbo_department e WITH(NOLOCK) on b.SalesDept=e.id
--left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
--left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh2 and isnull(d1.code,'')=:wh1 and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_TransferIn set descflexfield_pubdescseg50='T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询调拨入库单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询调拨入库单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询调拨入库单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms

            addDataDict = {
                "mf": itm,
                "tf": tf_data
            }
            # 拼接表头数据
            addData = wmsApiList.diaoBo(addDataDict)
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateTransferOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            add(db, logData)
    return


# 成品入库单
def u9capiCpRk(db, taskObj):
    print('u9capi成品入库单')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code from Complete_RcvRptDoc a WITH(NOLOCK)
left join Complete_RcvRptDocLine b WITH(NOLOCK) on a.id= b.RcvRptDoc
where isnull(a.descflexfield_pubdescseg50,'')!='T' 
and a.DocState!=2
and a.org='{0}'
group by a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep, 'MM' as request_id, a.id ,'OWNER' as owner_code,'04' as order_type,a.id as oth_task_id,'u9c' as ref_id, 
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,case when isnull(e.code,'')='' then '00000000' else e.code end as department,
'' as supplier_code,'' as supplier_name,'{1}' as operator_code, '' as source_bilno from Complete_RcvRptDoc a WITH(NOLOCK)
left join Complete_RcvRptDocLine b WITH(NOLOCK) on a.id= b.RcvRptDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.Item=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on a.RcvDep=e.id
--left join CBO_Customer f WITH(NOLOCK) on  a.Customer_Customer=f.id
--left join CBO_Customer_trl f1 WITH(NOLOCK) on  f.id=f1.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select 'MO' as source_bilid,'MM' as request_id,f.id as u9sourceid,f.docno as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
f.id as source_bilitm,b.id,c.code as item_code,c.name as item_name,cast(b.RcvQtyByWhUOM as float) as qty,
a.docno as request_code,b.id as request_itm,f.docno as source_bilno,b.RcvLotNo as batch_code from Complete_RcvRptDoc a WITH(NOLOCK)
left join Complete_RcvRptDocLine b WITH(NOLOCK) on a.id= b.RcvRptDoc
left join cbo_itemmaster c WITH(NOLOCK) on b.Item=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on a.RcvDep=e.id
left join MO_MO f WITH(NOLOCK) on  b.MO=f.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'') =:dep and a.org='{0}'
""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update Complete_RcvRptDoc set descflexfield_pubdescseg50='T' where id=:id"

    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm={}, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询成品入库单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:

        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None, parm=itm_bin, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询成品入库单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询成品入库单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新入库' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return

            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)

            add(db, logData)
    return


def u9capiCw(db, taskObj):
    print('u9capi储位资料')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = 'cw' + caseGetPrimaryKeyData()
    data = {
        "cw_no": logData['order'],
        "prd_name": "*****" + caseGetPrimaryKeyData()
    }
    logData['data'] = json.dumps(data, ensure_ascii=False)
    add(db, logData)
    return


def u9capiSalm(db, taskObj):
    print('u9capi业务员资料')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = 'sal' + caseGetPrimaryKeyData()
    data = {
        "sal_no": logData['order'],
        "prd_name": "*****" + caseGetPrimaryKeyData()
    }
    logData['data'] = json.dumps(data, ensure_ascii=False)
    add(db, logData)
    return


# 托工送检
def u9capiTwTi(db, taskObj):
    print('u9capi托工送检')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = 'twti' + caseGetPrimaryKeyData()
    data = {
        "twti": logData['order'],
        "prd_name": "*****" + caseGetPrimaryKeyData()
    }
    logData['data'] = json.dumps(data, ensure_ascii=False)
    add(db, logData)
    return


# 托工发料（借用调入单）
def u9capiTwDbCk(db, taskObj):
    print('u9capi托工调拨入库单*********************************************')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from InvDoc_TransferIn a WITH(NOLOCK)
left join InvDoc_TransInLine b WITH(NOLOCK) on a.id= b.TransferIn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join InvDoc_TransInDocType d WITH(NOLOCK) on d.id=a.TransInDocType
where isnull(d.code,'')='WWFL001' and isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,a.id,'TW' as request_id,a.id as oth_task_id,'OWNER' as owner_code,'07' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh2,isnull(d1.code,'') as wh1,
CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code2,
case when isnull(d.code,'')='' then '00000000' else d1.code end as warehouse_code1,
case when isnull(e.code,'')='' then '00000000' else e.code end  as department,'' as cus_no,'' as cus_name,'{1}' as operator_code,
 '' as source_bilno from InvDoc_TransferIn a WITH(NOLOCK)
left join InvDoc_TransInLine b WITH(NOLOCK) on a.id= b.TransferIn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.TransInWh=d.id
left join InvDoc_TransInSubLine dz WITH(NOLOCK) on b.id = dz.TransInLine 
left join cbo_wh d1 WITH(NOLOCK) on dz.TransOutWh=d1.id
left join cbo_department e WITH(NOLOCK) on b.SalesDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  b.OrderBy_Customer=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id,a.createdon,a.docno,d.code,e.code,d1.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select '' as source_bilid,'TW' as request_id,'' as u9sourceid,'' as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,b.LotInfo_LotCode as batch_code,a.docno as request_code,
b.id as request_itm,'' as source_bilno from InvDoc_TransferIn a WITH(NOLOCK)
left join InvDoc_TransInLine b WITH(NOLOCK) on a.id= b.TransferIn
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.TransInWh=d.id
left join InvDoc_TransInSubLine dz WITH(NOLOCK) on b.id = dz.TransInLine 
left join cbo_wh d1 WITH(NOLOCK) on dz.TransOutWh=d1.id
left join cbo_department e WITH(NOLOCK) on b.SalesDept=e.id
--left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
--left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh2 and isnull(d1.code,'')=:wh1 and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_TransferIn set descflexfield_pubdescseg50='T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询调拨入库单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询调拨入库单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询调拨入库单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms

            addDataDict = {
                "mf": itm,
                "tf": tf_data
            }
            # 拼接表头数据
            addData = wmsApiList.diaoBo(addDataDict)
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateTransferOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)

            add(db, logData)
    return

# 废品处理杂发出库单
def u9capiFpZfCk(db, taskObj):
    print('u9capi废品处理杂发出库单')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code
from InvDoc_MiscShip a WITH(NOLOCK)
left join InvDoc_MiscShipL b WITH(NOLOCK) on a.id= b.MiscShip
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join InvDoc_MiscShipDocType d WITH(NOLOCK) on d.id=a.MiscShipDocType
where isnull(d.code,'')='FPCL001' and isnull(a.descflexfield_pubdescseg50,'')!='T'and a.Status=1
and a.org='{0}'
group by a.id,a.createdon,a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep,a.id,'BCK' as request_id,a.id as oth_task_id,'OWNER' as owner_code,'12' as order_type,'u9c' as ref_id,CONVERT(VARCHAR(100), a.createdon, 23) as delivery_order_date,
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,isnull(d.code,'') as wh,
CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as schedule_date, a.docno as code,
case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,
case when isnull(e.code,'')='' then '00000000' else e.code end  as department,'' as cus_no,'' as cus_name,'{1}' as operator_code,
 '' as source_bilno from InvDoc_MiscShip a WITH(NOLOCK)
left join InvDoc_MiscShipL b WITH(NOLOCK) on a.id= b.MiscShip
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  b.OrderBy_Customer=f.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id,a.createdon,a.docno,d.code,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select b.LotInfo_LotCode as batch_code,'' as source_bilid,'BCK' as request_id,'' as u9sourceid,'' as u9sourceno,g.code as u9projectid,g1.name as u9projectnm,
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,a.docno as request_code,
b.id as request_itm,'' as source_bilno from InvDoc_MiscShip a WITH(NOLOCK)
left join InvDoc_MiscShipL b WITH(NOLOCK) on a.id= b.MiscShip
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join SM_SO f WITH(NOLOCK) on  b.SrcDoc=f.id
--left join SM_SOLine f1 WITH(NOLOCK) on  b.SrcDoc=f1.SO and b.SrcDocLine=f1.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'')=:dep and a.org='{0}'
""".format(org)

    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_CKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_MiscShip set descflexfield_pubdescseg50='T' where id=:id"

    # 1、查询需要推送的数据
    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm=None, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询杂发出库单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:
        print("itm_bin", itm_bin)
        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None,
                              parm=itm_bin,
                              db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询杂发出库单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询杂发出库单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新部门' + itm["code"] + '推送日期异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return
            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfCkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfCkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateDeliveryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)

            add(db, logData)
    return

def u9capiWtRk(db, taskObj):
    print('u9capi托工入库形态转换单单')
    logData = {}
    logData['status'] = True
    logData['name'] = taskObj["name"]
    logData['order'] = ""
    logData['data'] = ""
    logData['rem'] = ""

    # 获取拉取连接
    db_config = openDbBase()
    db_pull_session = get_yun_session(engineName=None, dbpool=db_config['u9capi'])
    userCode = u9cApiList.userCode
    # 获取org组织编码
    org, err = u9cApiList.orgCodeConfig(db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 获取token
    token, err = u9cApiList.getToken()
    if err != None:
        logData['status'] = False
        logData['rem'] = err
        add(db, logData)
        return
    # 查询拆分
    sql_select_bin = """
select  a.docno as code from InvDoc_TransferForm a WITH(NOLOCK)
left join InvDoc_TransferFormL b WITH(NOLOCK) on a.id= b.TransferForm
left join InvDoc_TransferFormDocType d WITH(NOLOCK) on d.id=a.TransferFormDocType
where isnull(d.code,'')='WWRK001' and isnull(a.descflexfield_pubdescseg50,'')!='T' 
and a.Status=1
and a.org='{0}'
group by a.docno
    """.format(org, userCode)

    sql_select_mf = """
select isnull(e.code,'') as dep, 'MM' as request_id, a.id ,'OWNER' as owner_code,'05' as order_type,a.id as oth_task_id,'u9c' as ref_id, 
CONVERT(VARCHAR(100), a.createdon, 23) as entry_order_date,CONVERT(VARCHAR(19), a.createdon, 120) as operate_time,CONVERT(VARCHAR(100), a.createdon, 23)  as expect_start_time, a.docno as code
,isnull(d.code,'') as wh,case when isnull(d.code,'')='' then '00000000' else d.code end as warehouse_code,case when isnull(e.code,'')='' then '00000000' else e.code end as department,
'' as supplier_code,'' as supplier_name,'{1}' as operator_code, '' as source_bilno from InvDoc_TransferForm a WITH(NOLOCK)
left join InvDoc_TransferFormL b WITH(NOLOCK) on a.id= b.TransferForm
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join CBO_Customer f WITH(NOLOCK) on  a.Customer_Customer=f.id
--left join CBO_Customer_trl f1 WITH(NOLOCK) on  f.id=f1.id
where a.docno=:code and isnull(a.descflexfield_pubdescseg50,'')!='T'
and a.org='{0}'
group by a.id , a.createdon ,a.createdon , a.docno 
,d.code ,e.code ,d.code ,e.code
    """.format(org, userCode)
    # 查询需要推送的数据
    sql_select = """
select 'TW' as source_bilid,'MM' as request_id,g.code as u9projectid,g1.name as u9projectnm,
'' as source_bilitm,'' as u9sourceid,'' as u9sourceno,'' as source_bilno,
--f.id as source_bilitm,f.id as u9sourceid,f.docno as u9sourceno,f.docno as source_bilno
b.id,c.code as item_code,c.name as item_name,cast(b.StoreUOMQty as float) as qty,
a.docno as request_code,b.id as request_itm,b.LotInfo_LotCode as batch_code from InvDoc_TransferForm a WITH(NOLOCK)
left join InvDoc_TransferFormL b WITH(NOLOCK) on a.id= b.TransferForm
left join cbo_itemmaster c WITH(NOLOCK) on b.ItemInfo_ItemID=c.id
left join cbo_wh d WITH(NOLOCK) on b.wh=d.id
left join cbo_department e WITH(NOLOCK) on b.HandleDept=e.id
--left join MO_MO f WITH(NOLOCK) on  b.MO=f.id
left join CBO_Project g WITH(NOLOCK) on g.id=b.Project
left join CBO_Project_trl g1 WITH(NOLOCK) on g.id=g1.id and g1.SysMLFlag='zh-CN'
where a.docno=:code
and isnull(d.code,'')=:wh and isnull(e.code,'') =:dep and a.org='{0}'
""".format(org)
    # 判断条码中数据是否存在
    sql_z = "select tz_no from MF_RKTZ_Z where ALLDH=:oth_task_id"
    # 更新推送时间
    sql_u = "update InvDoc_TransferForm set descflexfield_pubdescseg50='T' where id=:id"

    dataBin, err = dbListJon(sql=sql_select_bin, engineName=None, parm={}, db_session=db_pull_session)
    if err != None:
        logData['status'] = False
        logData['rem'] = '查询形态转换单表头汇总' + sql_select_bin + err
        add(db, logData)
        return
    for itm_bin in dataBin:

        # 1、查询需要推送的数据
        data, err = dbListJon(sql=sql_select_mf, engineName=None, parm=itm_bin, db_session=db_pull_session)
        if err != None:
            logData['status'] = False
            logData['rem'] = '查询形态转换单表头' + sql_select_mf + err
            add(db, logData)
            return
        for index, itm in enumerate(data):
            itm['oth_task_id'] = itm["code"] + '-' + str(index + 1)
            print(itm['oth_task_id'])
            oneData, err = dbJonOne(sql=sql_z, engineName=None, parm=itm, db_session=db)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询形态转换单表身' + sql_z + itm["code"] + '异常;' + err
                add(db, logData)
                continue
            # 更新推送标识
            _, err = dbEdit(sql=sql_u, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '更新入库' + itm["code"] + '推送标识异常;' + err
                add(db, logData)
                continue
            # descflexfield_pubdescseg50 = nowData()
            # u_data = [
            #   {
            #     "Org": {
            #       "Id": itm["org"],
            #       "Name": ""
            #     },
            #     "Code": itm["code"],
            #     "DescFlexField": {
            #       "PubDescSeg50": 'T'
            #     },
            #   }
            # ]
            # print(u_data)
            # # 更新u9c
            # u9cApiList.deptUpdate(u_data,token)

            # 查询表身需要推送的数据
            tf_data, err = dbListJon(sql=sql_select, engineName=None, parm=itm, db_session=db_pull_session)
            if err != None:
                logData['status'] = False
                logData['rem'] = '查询表身推送数据列表异常' + sql_select + err
                add(db, logData)
                return

            # 写入wms
            # 拼接表头数据
            addData = wmsApiList.mfRkTz(itm)
            # 拼接表身数据
            tfData = wmsApiList.tfRkTz(itm["code"], tf_data)
            addData['order_lines'] = tfData
            logData['order'] = itm["code"]

            logData['data'] = json.dumps(addData, ensure_ascii=False)
            if oneData is None:
                # 不存存在需要新增
                print('不存在')
                logData['rem'] = '新增'
                _, err = wmsApiList.apiComm(addData, 'SYS/CreateEntryOrder')
                if err != None:
                    logData['status'] = False
                    logData['rem'] = err
                    add(db, logData)
                    continue
            else:
                logData['status'] = False
                logData['rem'] = '单据存在'
                add(db, logData)
                # # 存在需要更新
                # print('存在')
                # logData['rem'] = '数据异动更新'
                # _, err = wmsApiList.apiComm(addData, 'SYS/UpdateDepartment')
                # if err != None:
                #     logData['status'] = False
                #     logData['rem'] = err
                #     add(db, logData)
                #     continue
            logData['rem'] = ""
            logData['status'] = True
            logData['order'] = itm["code"]
            logData['data'] = json.dumps(addData, ensure_ascii=False)

            add(db, logData)
    return
